﻿
Partial Class WebForm_ManageWorkContent_SearchWorkHeader
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.InitData()
        End If
    End Sub

    Protected Sub ddlStatus_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStatus.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub ddlOrganization_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlOrganization.SelectedIndexChanged
        Me.BindGV()
    End Sub

    Protected Sub gv_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.SelectedIndexChanged
        Me.SelectedStatus = Me.ddlStatus.SelectedValue
        RaiseEvent SelectedIndexChanged(Me, New EventArgs)
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.BindGV()
    End Sub
End Class

Partial Class WebForm_ManageWorkContent_SearchWorkHeader
    Private Sub InitData()
        Me.BindDDLStatus()
        Me.BindDDLOrganization()
        Me.BindDDLDataSource()
    End Sub

    Private Sub BindDDLOrganization()
        Me.ddlOrganization.DataTextField = "OrganizationName"
        Me.ddlOrganization.DataValueField = "UOrganizationID"
        Me.ddlOrganization.DataSource = MyObj.GetDDLOrganizationData()
        Me.ddlOrganization.DataBind()
    End Sub

    Private Sub BindDDLDataSource()
        Me.ddlCreateType.DataTextField = "Text"
        Me.ddlCreateType.DataValueField = "Value"
        Me.ddlCreateType.DataSource = MyObj.GetCodeMappingData("WorkHeader", "CreateType")
        Me.ddlCreateType.DataBind()
    End Sub

    Private Sub BindDDLStatus()
        Dim dt As Data.DataTable

        dt = MyObj.GetCodeMappingData("WorkRecord", "Status")

        Me.ddlStatus.DataTextField = "Text"
        Me.ddlStatus.DataValueField = "Value"
        Me.ddlStatus.DataSource = dt
        Me.ddlStatus.DataBind()
    End Sub

    Public Sub BindGV()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.AppendFormat("select a.*,u.UserName as VolunteerName, u.VOrganizationID,o.OrganizationName,CreateDate,Status")
        sql.AppendFormat(" from WorkHeader a")
        sql.AppendFormat(" inner join OrganizationView o on a.UOrganizationID = o.UOrganizationID ")
        sql.AppendFormat(" inner join [User] u on a.CreateBy = u.UserID ")
        sql.AppendFormat(" inner join OrganizationTree ot on a.UOrganizationID = ot.UOrganizationID ")
        sql.AppendFormat(" where 1=1 ")
        sql.AppendFormat(Me.GetWhereSQL())
        sql.AppendFormat(" order by CreateDate")
        dt = dat.GetData(sql)

        Me.gv.DataSource = dt
        Me.gv.DataBind()

        RaiseEvent DataBound(Me, New EventArgs)
        Me.gv.SelectedIndex = -1
    End Sub

    Private Function GetWhereSQL() As String
        Dim sql As New MyStringBuilder

        sql.AppendFormat(" and a.Status = '{0}' ", Me.ddlStatus.SelectedValue)
        sql.AppendFormat(" and ot.URoot = '{0}' ", Me.ddlOrganization.SelectedValue)
        sql.AppendFormat(" and a.CreateType = '{0}' ", Me.ddlCreateType.SelectedValue)

        Return sql.ToString
    End Function

    Public WriteOnly Property SelectedIndex() As Integer
        Set(ByVal value As Integer)

            If Me.gv.Rows.Count > 0 Then
                Me.gv.SelectedIndex = value
                RaiseEvent SelectedIndexChanged(Me, New EventArgs)
            End If

        End Set
    End Property

    Public Property SelectedStatus() As String
        Get
            Return ViewState("SelectedStatus")
        End Get
        Set(ByVal value As String)
            ViewState("SelectedStatus") = value
        End Set
    End Property

    Public Property CreateType() As String
        Get
            Return ViewState("CreateType")
        End Get
        Set(ByVal value As String)
            ViewState("CreateType") = value
        End Set
    End Property

    Public ReadOnly Property SelectedWorkHeaderID() As String
        Get
            If Me.gv.SelectedRow Is Nothing Then
                Return Me.gv.DataKeys(0)("WorkHeaderID")
            Else
                Return Me.gv.DataKeys(Me.gv.SelectedRow.RowIndex)("WorkHeaderID")
            End If
        End Get
    End Property

    Public ReadOnly Property Count() As Integer
        Get
            Return Me.gv.Rows.Count
        End Get
    End Property

    Public Event SelectedIndexChanged(ByVal source As Object, ByVal e As EventArgs)
    Public Event DataBound(ByVal source As Object, ByVal e As EventArgs)
End Class